﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Navigation;
using YouFood.ServiceReference1;
using YouFood.Web;
using YouFood.Web.Model;
using StatDto = YouFood.Web.Model.StatDto;

namespace YouFood.Views
{
    public partial class AdminView : Page, INotifyPropertyChanged
    {
        public AdminView()
        {
            InitializeComponent();
            DataContext = this;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        private ObservableCollection<StatDto> _statDtos;
        public ObservableCollection<StatDto> StatDtos
        {
            get { return _statDtos; }
            set
            {
                _statDtos = value;
                NotifyPropertyChanged("StatDtos");
            }
        }
        private double _totalPrice;
        public double TotalPrice
        {
            get { return _totalPrice; }
            set
            {
                _totalPrice = value;
                NotifyPropertyChanged("TotalPrice");
            }
        }

        private OrderCookerDomainContext _context = new OrderCookerDomainContext();

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!WebContext.Current.User.IsInRole("Admin"))
                return; // Elle sont ou les acolade? Petit homosexuelle!!

            InitializeComponent();

            this.img1.Source = new BitmapImage(new Uri("/YouFood;component/Assets/Images/Report.jpg", UriKind.Relative));

            FinancialName.Visibility = System.Windows.Visibility.Collapsed;
            StatName.Visibility = System.Windows.Visibility.Collapsed;

        }

        private ServiceReference1.Service1Client service = new Service1Client();

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {

            service.GetStatCompleted += (s, se) =>
                                            {
                                                StatDtos = new ObservableCollection<StatDto>(se.Result.Select(a => new StatDto() {Count = a.Count, Name = a.Name}));
                                                StatName.Visibility = System.Windows.Visibility.Visible;
                                                FinancialName.Visibility = System.Windows.Visibility.Collapsed;

                                            };

            service.GetStatAsync();

            _context = new OrderCookerDomainContext();
            var query = _context.GetOrdersQuery();
            var loadData = _context.Load(query);
            loadData.Completed += new EventHandler(loadData_Completed);
        }

        void loadData_Completed(object sender, EventArgs e)
        {
            var s = (sender as LoadOperation<Order>).Entities;
        }

        private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
        {
            

            service.GetPerformanceCompleted += (i, ie) =>
                                                   {
                                                       TotalPrice = ie.Result;
                                                   };
            service.GetPerformanceAsync();

            FinancialName.Visibility = System.Windows.Visibility.Visible;
            StatName.Visibility = System.Windows.Visibility.Collapsed;
        }

    }
}
